﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WsProtocol.PublicClass
{
    //帧格式类  所有协议帧格式的基类
    public class FrameFormat
    {
        public string Data = ""; //数据内容
        public List<PtlPubConsts.TByteFormat> DataList = new List<PtlPubConsts.TByteFormat>();

        public void SetBaseData(string sdata)
        {
            Data = sdata;
        }

        //增加格式
        public void AddFormat(PtlPubConsts.TByteFormat newformat)
        {
            DataList.Add(newformat);
        }

        //删除格式
        public void RemoveFormat(PtlPubConsts.TByteFormat newformat)
        {
            for (int i = DataList.Count - 1; i >= 0; i--)
            {
                if (DataList[i] == newformat)
                    DataList.Remove(DataList[i]);
            }
        }

        //清空格式
        public void ClearFormat()
        {
            DataList.Clear();
        }


        //克隆下级节点格式中所有可复制的
        public int CloneFormat()
        {
            int copynodecount = 0;  //复制的节点个数
            for (int i = 0; i < DataList.Count; i++)
            {
                PtlPubConsts.TByteFormat xe = DataList[i];
                if (!xe.IfRepeat)
                    continue;
                copynodecount++;
                PtlPubConsts.TByteFormat newson = (PtlPubConsts.TByteFormat)xe.Copy();
                newson.ItemName = newson.AddNOOnce(newson.ItemName, -1, 1, true);  //名称中的序号+1

                newson.ItemData = "";
                newson.ItemValue = "";


                DataList.Add(newson);

                xe.IfRepeat = false; //将原来的可重复节点置为不可重复，重复节点由复制出的新节点代替

            }
            return copynodecount;

        }
    }
}
